package io.codegeneratetool.web.service;

 
import java.beans.PropertyVetoException;

import io.codegeneratetool.web.entity.DBConnection;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DataSourceFactory {

	private static ComboPooledDataSource dataSource ;
	public static DataSource createDataSource(DBConnection connection){
		if(dataSource!=null){
			dataSource.close();
			dataSource=null;
		}
		  dataSource =  new ComboPooledDataSource();
		try {
			dataSource.setDriverClass(connection.getDrivername());
		} catch (PropertyVetoException e) {
			 
			e.printStackTrace();
		}
		dataSource.setUser( connection.getUsername());
		dataSource.setPassword(connection.getPasswd());
		dataSource.setJdbcUrl( connection.getJdbcurl());
		dataSource.setMinPoolSize(3);
		dataSource.setInitialPoolSize(10);
		dataSource.setMaxPoolSize(20);
		dataSource.setTestConnectionOnCheckin(false);
		dataSource.setTestConnectionOnCheckout(false);
		dataSource.setAcquireIncrement(5);  //连接池耗尽,获取欣连接数
		dataSource.setAcquireRetryAttempts(5);
		dataSource.setMaxIdleTime(0);  //如果不丢弃的话  应对经常出现connection reset 错误,测试一段时间
		
		return dataSource;
		
		
		
	}
	
	
}
